package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.ui.util.GDCharInfoList;

import javax.swing.*;
import java.awt.*;

public class GDCharDataOverviewPane extends AdjustablePanel {
    private GDCharStatEditPane pnlEdit;
    private GDCharFactionPane pnlFactions;
    private GDCharDevotionPane pnlDevotions;
    private GDCharStatAchievementPane pnlAchieve;
    private JPanel pnlMain;
    private GDCharEditTabbedPane tabPane;
    private GDCharInfoList.GDCharFileInfo info;

    public GDCharDataOverviewPane(final GDCharEditTabbedPane tabPane) {
        this.tabPane = tabPane;
        this.adjustUI();
        this.setChar(null);
        final JPanel pnlLeft = this.buildLeftSidePanel();
        final JPanel pnlRight = this.buildRightSidePanel();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(pnlLeft)).addGroup(layout.createParallelGroup().addComponent(pnlRight));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(pnlLeft).addComponent(pnlRight));
        layout.setVerticalGroup(vGroup);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntButton = UIManager.getDefaults().getFont("Button.font");
        if (fntButton == null) {
            fntButton = fntLabel;
        }
        Font fntCombo = UIManager.getDefaults().getFont("ComboBox.font");
        if (fntCombo == null) {
            fntCombo = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntButton = fntButton.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCombo = fntCombo.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        GDCharInfoList.adjustCharInfos(null, null);
        if (this.pnlEdit == null) {
            this.pnlEdit = new GDCharStatEditPane(this.tabPane);
        } else {
            this.pnlEdit.adjustUI();
        }
        if (this.pnlFactions == null) {
            this.pnlFactions = new GDCharFactionPane();
        } else {
            this.pnlFactions.adjustUI();
        }
        if (this.pnlDevotions == null) {
            this.pnlDevotions = new GDCharDevotionPane();
        } else {
            this.pnlDevotions.adjustUI();
        }
        if (this.pnlAchieve == null) {
            this.pnlAchieve = new GDCharStatAchievementPane();
        } else {
            this.pnlAchieve.adjustUI();
        }
    }

    private JPanel buildLeftSidePanel() {
        final JPanel panel = new JPanel();
        final JPanel pnlWrapAchieve = this.buildWrapAchievePanel();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlEdit).addComponent(pnlWrapAchieve));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlEdit)).addGroup(layout.createParallelGroup().addComponent(pnlWrapAchieve));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.pnlEdit, pnlWrapAchieve);
        return panel;
    }

    private JPanel buildWrapAchievePanel() {
        final JPanel panel = new JPanel();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlAchieve));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlAchieve));
        layout.setVerticalGroup(vGroup);
        return panel;
    }

    private JPanel buildRightSidePanel() {
        final JPanel panel = new JPanel();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlFactions)).addGroup(layout.createParallelGroup().addComponent(this.pnlDevotions));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlFactions).addComponent(this.pnlDevotions));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(1, this.pnlFactions, this.pnlDevotions);
        return panel;
    }

    public void setChar(final GDChar gdc) {
        this.pnlEdit.setChar(gdc);
        this.pnlFactions.setChar(gdc);
        this.pnlDevotions.setChar(gdc);
        this.pnlAchieve.setChar(gdc);
    }

    public void updateChar(final GDChar gdc) {
        this.pnlEdit.updateChar(gdc);
        this.pnlFactions.updateChar(gdc);
        this.pnlDevotions.updateChar(gdc);
        this.pnlAchieve.updateChar(gdc);
    }

    public String getCharName() {
        if (this.pnlEdit == null) {
            return null;
        }
        return this.pnlEdit.getCharName();
    }

    public void updateConfig() {
        if (this.pnlFactions != null) {
            this.pnlFactions.updateConfig();
        }
    }

    public void setSkillPoints(final int points) {
        if (this.pnlEdit == null) {
            return;
        }
        this.pnlEdit.setSkillPoints(points);
    }

    public void resetMastery(final String masteryID) {
        if (this.pnlEdit != null) {
            this.pnlEdit.resetMastery(masteryID);
        }
    }
}
